package com.twm.register.impl;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.crypto.SecureUtil;
import com.twm.common.ResponseMessage;
import com.twm.common.StatusCode;
import com.twm.entity.TuserAccount;
import com.twm.entity.TuserAccountWithBLOBs;
import com.twm.mapper.TuserAccountMapper;
import com.twm.redis.RedisService;
import com.twm.register.LoginService;
import com.twm.util.EncryptionUtil;
import com.twm.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.UUID;

/**
 * @ClassName LoginServiceImpl
 * @Description TODO
 * @Author twm17
 * @Date 2020/11/11 17:54
 * @Version 1.0
 */
@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private TuserAccountMapper accountMapper;

    @Override
    public ResponseMessage login(String account, String password) {

        TuserAccountWithBLOBs user = accountMapper.findUserByAccount(account);
        if(Objects.isNull(user)) {
            return new ResponseMessage(StatusCode.NAME_INVALID.getStatus(),StatusCode.NAME_INVALID.getMessage());
        }
        String decode = EncryptionUtil.decode(user.getPassword());
        if(password.equals(decode)) {
            return new ResponseMessage(StatusCode.PASSWORD_ERROR.getStatus(),StatusCode.PASSWORD_ERROR.getMessage());
        }
        UserVo userVo = new UserVo();
        userVo.setUserName(user.getUserName());
        userVo.setUserId(user.getId());
        userVo.setAreaId(user.getAreaId());
        return new ResponseMessage(userVo);
    }

    @Override
    public CircleCaptcha getImageCheckCode() {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        return captcha;
    }
}
